/*! 
 * jquery.autoscroll - v 1.0.0 - http://jquery-ux.com/
 * Copyright (c) 2010 Michael Helgeson, Three Dub Media
 * Open Source MIT License - http://jquery-ux.com/license 
 */
// Created: 2009-04-14 
// Updated: 2010-01-08
// REQUIRES: jquery 1.3+

;(function( $ ){ // confine scope

$.fn.autoScroll = function( arg, opts ){
	opts = opts || {};
	// the main element
	var elem = this[0],
	// the coordinates to calculate 
	XX = arg.pageX || arg.left || arg[0] || 0,
	YY = arg.pageY || arg.top || arg[1] || 0,
	// the optional options...
	buffer = opts.buffer || 100, // pixels from edge
	dist = opts.distance || 20, // pixels to scroll per call
	// page/document scrolling...
	page = !elem.ownerDocument || $.nodeName(elem,'html'),
	// the element to utilize
	$elem = page ? $( window ) : this.eq(0),	
	// the parameters to modify...
	top = $elem.scrollTop(), left = $elem.scrollLeft(),
	// attributes to calculate position
	offset = page ? { top:0, left:0 } : $elem.offset(),
	// calculate the distance to move...
	vert = ( YY - offset.top - ( page ? top : 0 ) ) < buffer ? -dist : 
		$elem.height() + offset.top - YY - ( page ? top : 0 ) < buffer ? dist : 0,
	hori = ( XX - offset.left - ( page ? left : 0 ) ) < buffer ? -dist : 
		$elem.width() + offset.left - XX - ( page ? left : 0 ) < buffer ? dist : 0;
	// set the vertical scroll
	if ( !!vert ) $elem.scrollTop( top + vert );
	// set the horizontal scroll
	if ( !!hori ) $elem.scrollLeft( left + hori );
	// preserve jquery chain...
	return this;
	};

})( jQuery ); // secure the $ jQuery alias